package cn.fl.car300.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.car300.api.IInfCar300RecApiService;
import cn.fl.car300.condition.InfCar300RecQueryCondition;
import cn.fl.car300.constant.InfCar300RecConstant;
import cn.fl.car300.dto.InfCar300RecDTO;
import cn.fl.car300.service.IInfCar300RecService;
import cn.fl.car300.transform.InfCar300RecVOTransform;
import cn.fl.car300.vo.InfCar300RecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.upload.constant.UploadConstant;

/**
 * @Description: 二手车评估表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class InfCar300RecServiceImpl implements IInfCar300RecService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IInfCar300RecService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IInfCar300RecApiService infCar300RecApiService;

	@Override
	public InfCar300RecVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		InfCar300RecDTO dto = null;//this.自定义ApiService.selectById(id);
		return InfCar300RecVOTransform.toVO(dto);
	}

	@Override
	public Long add(InfCar300RecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.infCar300RecApiService.add(InfCar300RecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infCar300RecApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(InfCar300RecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infCar300RecApiService.modify(InfCar300RecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.infCar300RecApiService.searchListPage(page);
	}
	
	@Override
    public List<InfCar300RecVO> searchList(InfCar300RecQueryCondition condition) throws BaseException{
		if(null == condition) return new ArrayList<InfCar300RecVO>();
		List<InfCar300RecDTO> list = this.infCar300RecApiService.searchList(condition);
		return InfCar300RecVOTransform.toVOList(list);
	}
	
	@Override
	public void makeOldCarReport(InfCar300RecQueryCondition condition) throws BaseException {
		//拼接存入路径-------->系统相对路径前缀    + 系统代码  + 模块(自定义)  + 时间   + 文件
		String prifixPath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_ROOT_PATH);//根目录
		StringBuffer serverPath = new StringBuffer(prifixPath);//最终保存路径
		String filePath = InfCar300RecConstant.UPLOAD_DIR;//模块(自定义)
		if(!serverPath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"结尾,不是加入斜杠
			serverPath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
    	}
		serverPath.append(PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_COSTOMER_PATH));//自定义代码-其中包括系统代码
		if(StringUtils.isNotEmpty(filePath)){//如果不等于空则加入
			if(filePath.startsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"开头
				serverPath.append(filePath.substring(1));
			}else{
				serverPath.append(filePath);
			}
		}
		if(!serverPath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"结尾
			serverPath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
    	}
		String currentTime = DateUtils.formatDateToString(new Date(), DateUtils.YYYYMMDD);
		serverPath.append(currentTime);
	    //检查目录
	    File pathFile = new File(serverPath.toString());
	    if (!pathFile.exists()) {
	    	//文件夹不存 创建文件
	    	boolean createPath = pathFile.mkdirs();
	    	if(!createPath){
	    		throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "服务器附件目录未创建成功!");
	    	}
	    }
	    // 定义上传路径
        String path = serverPath.toString();
	    String savePath = path.replace(prifixPath, "");
	    condition.setSavePath(savePath);//DB存储路径
	    condition.setServerPath(path);//服务器保存路径
		this.infCar300RecApiService.makeOldCarReport(condition);
	}
}
